﻿using MvcApplication.Common;
using MvcApplication.Entity;
using MvcApplication.Interface;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace MvcApplication.Controllers
{
    public class CategoryController : Controller
    {
        public ICategoryService _categoryService;

        public CategoryController(ICategoryService categoryService)
        {
            _categoryService = categoryService;
        }

        [Permission(Entity.Menu.类目管理, Entity.PermissionType.访问)]
        public ActionResult List(CategoryFilter filter, Pager page)
        {
            var datas = _categoryService.GetCategories(filter, page);
            return View(datas);
        }

        [Permission(Entity.Menu.类目管理, Entity.PermissionType.访问)]
        public JsonResult Index(CategoryFilter filter, Pager page)
        {
            page.PageSize = 1000;
            var datas = _categoryService.GetCategories(filter, page);
            return Json(datas, JsonRequestBehavior.AllowGet);
        }

        [Permission(Entity.Menu.类目管理, Entity.PermissionType.编辑)]
        [HttpPost]
        public JsonResult Save(Category category)
        {
            var data = _categoryService.Save(category);
            return Json(data);
        }

        [Permission(Entity.Menu.类目管理, Entity.PermissionType.删除)]
        [HttpPost]
        public JsonResult Delete(List<long> categoryIDs)
        {
            _categoryService.Delete(categoryIDs);
            return Json(true);
        }

        [Permission(Entity.Menu.类目管理, Entity.PermissionType.访问)]
        public JsonResult GetCategory(long id)
        {
            var data = _categoryService.GetCategory(id);
            return Json(data, JsonRequestBehavior.AllowGet);
        }
    }
}